package com.plant.classify.controller;

import com.plant.classify.domain.Classif;
import com.plant.classify.service.ClassifyService;
import com.plant.classify.vo.*;
import com.plant.classify.vo.OrderVo;
import com.plant.classify.vo.productVo;
import com.plant.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 华景壘
 * @date: 2024/8/21 19:56
 * @description:
 */
@RestController
@RequestMapping("/FlowerCity")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    /**
     * 分类查询
     * @return
     */
    @GetMapping("/findByClassify")
    public Result getClassify() {
        List<Classif> list = classifyService.getClassify();
        return Result.ok(list);
    }


    /**
     * 查询商品
     * @param id
     * @return
     */
    @GetMapping("/findByGoods/{id}")
    public Result findByGoods(@PathVariable("id") Integer id) {
        List<productVo> list = classifyService.findByGoods(id);
        return Result.ok(list);
    }

    /**
     * 保存加入购物车信息
     * @param orderVo
     */
    @PostMapping("/setCart")
    public void setCart(@RequestBody OrderVo orderVo) {
        classifyService.setCart(orderVo);
    }

    /**
     * 拉取redis购物车信息
     * @return
     */
    @GetMapping("/findAllToCart/{uid}")
    public Result findAllToCart(@PathVariable("uid") Integer uid) {
        System.out.println(uid);
        List<OrderVo> list = classifyService.findAllToCart(uid);
        return Result.ok(list);
    }

    /**
     * 订单添加
     * @param orderInsert
     * @return
     */
    @PostMapping("/orderDetail")
    public Result orderDetail(@RequestBody OrderInsert orderInsert) {
        Integer i = classifyService.orderDetail(orderInsert);
        if (i==1){
            return Result.fail(400,"订单重复提交");
        }
        return Result.fail(200, "下单成功");
    }

    /**
     * 支付成功修改订单支付状态
     * @param orderId
     * @return
     */
    @GetMapping("/UpdateStatus/{orderId}")
    public Result UpdateStatus(@PathVariable String orderId) {
        classifyService.UpdateStatus(orderId);
        return Result.build(200, "修改支付状态成功");
    }

    /**
     * 查询对应用户地址
     * @param userId
     * @return
     */
    @GetMapping("/UserAddress/{userId}")
    public Result UserAddress(@PathVariable("userId") Integer userId) {
        AddressVo addressVo = classifyService.UserAddress(userId);
        return Result.ok(addressVo);
    }

    /**
     * B-分类新增
     * @param classif
     * @return
     */
    @PostMapping("/xin")
    public Result xin(@RequestBody Classif classif) {
        System.out.println(classif);
        classifyService.save(classif);
        return Result.build(200, "新增成功");
    }

    /**
     * B-分类查询
     * @param id
     * @return
     */
    @GetMapping("/getClassify/{id}")
    public Result getClassify(@PathVariable("id") Integer id) {
        Classif classif = classifyService.getClassifyId(id);
        return Result.ok(classif);
    }

    /**
     * B-分类修改
     * @param classif
     * @return
     */
    @PostMapping("/updateClassify")
    public Result updateClassify(@RequestBody Classif classif) {
        classifyService.updateClassify(classif);
        return Result.build(200, "修改成功");
    }


    @GetMapping("/findByDetail/{id}")
    public Result findByDetail(@PathVariable("id") Integer id) {
        productVo productVo = classifyService.findByDetail(id);
        return Result.ok(productVo);
    }
}
